package com.slt.concurrency.mashibing2019.juc.c_007_LockAndTools.exercise;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 提前unpark也可以，线程就不会park了
 */
public class LockSupport01 {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                if (i == 5) {
                    //阻塞当前线程
                    LockSupport.park();
                }
                System.out.println(i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

        //唤醒thead线程 继续执行
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //不需要睡眠也可以，这样就不会park了
        LockSupport.unpark(thread);
    }
}
